package gruppe7.servlets;

import gruppe7.model.Model;
import gruppe7.util.Html;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TicketPriceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    	Model model = Model.getInstance();
    	Html html = new Html();
    	PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp");

        out.println(html.setHeader("Lookup Ticket Price"));
        rd.include(request, response);

        out.println("<div id ='content'>");
        out.println("<div id='content-left'>");
        out.println("<h3><img src='images/tickets.png' /> Ticket Lookups</h3><br/>");
        
        String departure = (String) request.getParameter("departure");
        String destination = (String) request.getParameter("destination");
        
        if (departure.equals(destination)) {
        	RequestDispatcher forwardDisp = getServletContext().getRequestDispatcher("/lookup_price.jsp");
            request.setAttribute("same", true);
            forwardDisp.forward(request, response);
        }
            
        int totalFantoms = 0;
            
        try {
        	
        	int departureZone = model.getStationByName(departure).getZone();
            int destinationZone = model.getStationByName(destination).getZone();
                
            int totalZones = 0;

            if (departureZone > destinationZone) {
            	totalZones = departureZone - destinationZone;
            } else {
            	totalZones = destinationZone - departureZone;
            }
            
            totalFantoms = model.getFantoms(totalZones);
        }
            
        catch (Exception e) {
        	out.println("Database fejl: " + e.getMessage());
        }
                        
        out.println("<p> The price to travel from " + departure + " to " + destination + " is " + totalFantoms + " Fantoms. </br>"
        		+ "Have a safe trip!</p>");
            
        out.println("<form action='/gruppe7/purchase_ticket.do' method='post'>"
        		+ "<input type='hidden' id='departure' name='departure' value='" + departure + " '/>"
                + "<input type='hidden' id='destination' name='destination' value='" + destination + " '/>"
                + "<input type='hidden' id='departure' name='price' value='" + totalFantoms + " '/>"
                + "<input class='submit' type='submit'value='buy ticket'> "
                + "</form>");
            
        out.println("<br /><p><a href='/gruppe7/lookup_price.jsp'>Look up another price - click here</a></p>");

        out.println("</div></div>");
        out.println(html.setFooter());
    	
    }

}
